package com.pawernode.config;

import lombok.Data;
import org.springframework.amqp.core.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置类，用于声明交换机、队列和绑定关系
 */
@Configuration
@ConfigurationProperties(prefix = "my") // 指定配置文件中的属性前缀
@Data
public class RabbitConfig {
    private String exchangeName;
    private String queueAName;
    private String queueBName;

    /**
     * 创建一个主题交换机
     * @return TopicExchange实例
     */
    @Bean
    public TopicExchange topicExchange(){
        return ExchangeBuilder.topicExchange(exchangeName).build();
    }

    /**
     * 创建一个持久化的队列A
     * @return Queue实例
     */
    @Bean
    public Queue queueA(){
        return QueueBuilder.durable(queueAName).build();
    }

    /**
     * 创建一个持久化的队列B
     * @return Queue实例
     */
    @Bean
    public Queue queueB(){
        return QueueBuilder.durable(queueBName).build();
    }

    /**
     * 将队列A绑定到主题交换机，只接收匹配 "*.orange.*" 的消息
     * @param topicExchange 主题交换机
     * @param queueA 队列A
     * @return Binding实例
     */
    @Bean
    public Binding bindingA(TopicExchange topicExchange, Queue queueA) {
        return BindingBuilder.bind(queueA).to(topicExchange).with("*.orange.*");
    }

    /**
     * 将队列B绑定到主题交换机，只接收匹配 "*.orange.*" 的消息
     * @param topicExchange 主题交换机
     * @param queueB 队列B
     * @return Binding实例
     */
    @Bean
    public Binding bindingB1(TopicExchange topicExchange, Queue queueB) {
        return BindingBuilder.bind(queueB).to(topicExchange).with("*.orange.*");
    }

    /**
     * 将队列B绑定到主题交换机，接收匹配 "lazy.#" 的所有消息
     * @param topicExchange 主题交换机
     * @param queueB 队列B
     * @return Binding实例
     */
    @Bean
    public Binding bindingB2(TopicExchange topicExchange, Queue queueB) {
        return BindingBuilder.bind(queueB).to(topicExchange).with("lazy.#");
    }
}
